const app = getApp();
let fetch = require("../../utils/fetch");
Page({
    data: {
        showFloat: false,
        showInfo: false,
        urlAddress: app.globalData.imageUrl,
        imageUrl: app.globalData.imageUrl,
        balance: 0,
        resultList: [],
        isFrst: false,
        rechargeAmount: '',
        presentAmount: 0,
        isShowtips: false,
        isShowMaiXiang: false,
        MaiXiangMsg: ''

    },
    onLoad: function(options) {
        // 生命周期函数--监听页面加载
        console.log(options.balance)
        this.setData({
            balance: options.balance
        })
        fetch
            .getRechargePromotionList()
            .then(response => {
                const obj = response.data.obj;
                if (obj.resultList.length > 0) {
                    obj
                        .resultList
                        .forEach(e => {
                            e.isSelect = false
                        });
                }
                this.setData({
                    resultList: obj.resultList,
                    isFrst: obj.isFirst
                })

            })
            .catch(e => {
                fetch.showMyToast(this, e.data.des)
            })

    },

    onShow: function() {
        // 生命周期函数--监听页面显示


    },
    select: function(e) {
        console.log(e)
        const {
            rechargeamount,
            presentamount,
            index
        } = e.currentTarget.dataset;
        console.log(rechargeamount, presentamount, index);
        var resultList = this.data.resultList;
        console.log("select", this.data.resultList)
        resultList.forEach((e, i) => {
            e.isSelect = false;
            if (i == index) {
                e.isSelect = true;
                return
            }
        })
        this.setData({
            resultList,
            rechargeAmount: rechargeamount,
            presentAmount: presentamount,
            isShowtips: true
        })
    },
    bindKeyInput: function(e) {

        const resultList = this.data.resultList;
        resultList.forEach(e => {
            e.isSelect = false
        });
        console.log('resultList', resultList)
        this.setData({
            resultList
        })
        console.log(e.detail.value)
        console.log("&&&&&")
        if (e.detail.value <= 0) {
            wx.showToast({
                title: "充值要大于0元",
                icon: 'none',
                duration: 2000
            })
            this.setData({
                rechargeAmount: null
            })
        } else {
            this.setData({
                rechargeAmount: e.detail.value
            })

        }
    },
    getRechargeInfo: function() {
        if (!this.data.rechargeAmount) {
            return
        }
        var _self = this;
        var shopCode = '';
        if (getApp().globalData.common.shopCode) {
            shopCode = getApp().globalData.common.shopCode;
        }
        wx.request({
            url: getApp().globalData.baseUrl + '/applet/recharge/getRechargeHint.do',
            data: {
                platform: 6,
                shopCode: shopCode,
                rechargeAmount: this.data.rechargeAmount

            },
            header: getApp().globalData.header,
            success: function(res) {
                console.log(res)
                if (res.data.code == 200) {

                    _self.setData({
                        promLabel: res.data.obj.promLabel,
                        shopName: res.data.obj.shopName
                    })

                    _self.showInfoAction();
                } else {

                }
            }
        })
    },
    recharge: function() {
        if (this.data.rechargeAmount <= 0) {
            wx.showToast({
                title: "充值要大于0元",
                icon: 'none',
                duration: 2000
            })
        } else if (!this.data.isShowMaiXiang || this.data.rechargeAmount < 100) {
            this.setData({
                showFloat: false,
                showInfo: false
            })
            console.log("****2***")
            fetch.loadingText()
            fetch
                .createRechargeOrder(this.data.rechargeAmount)
                .then(response => {
                    fetch.hideLoadingText();
                    let that = this;
                    const res = response.data;
                    //type=1:微信代扣
                    console.log("****")
                    console.log(res)
                    if (res.obj.type == 0) {
                        //type=0有密码支付,未开通免密
                        wx.requestPayment({
                            'timeStamp': res.obj.timeStamp,
                            'nonceStr': res.obj.nonceStr,
                            'package': res.obj.prepay_id,
                            'signType': res.obj.signType,
                            'paySign': res.obj.paySign,
                            'success': function(response) {
                                wx.showToast({
                                    title: '充值成功,预计10分钟内到账',
                                    icon: 'none',
                                    duration: 2000,
                                    mask: true,
                                    'success': function() {
                                        setTimeout(() => {
                                            wx.switchTab({
                                                url: '../mine/mine'
                                            })
                                        }, 2000)
                                    }
                                })

                            },
                            'fail': function(res) {
                                console.log(res)
                                wx.showToast({
                                    title: '取消支付',
                                    icon: 'none',
                                    duration: 1500
                                })
                            }
                        })
                    } else if (res.obj.type == 1) {
                        //type=1:微信代扣
                        console.log("****")
                        console.log('麦享')
                        if (res.obj.status == 1) {
                            wx.showToast({
                                title: '充值成功,预计10分钟内到账',
                                icon: 'none',
                                duration: 2000,
                                mask: true,
                                'success': function() {
                                    setTimeout(() => {
                                        wx.switchTab({
                                            url: '../mine/mine'
                                        })
                                    }, 2000)
                                }
                            })
                        } else if (res.obj.status == 4) {
                            wx.showToast({
                                title: '支付中...请稍后查看余额明细',
                                icon: 'none',
                                duration: 2000,
                                mask: true,
                                'success': function() {
                                    setTimeout(() => {
                                        wx.switchTab({
                                            url: '../mine/mine'
                                        })
                                    }, 2000)
                                }
                            })
                        } else {
                            wx.showToast({
                                title: '支付失败',
                                icon: 'none',
                                duration: 2000,
                                mask: true,
                                'success': function() {

                                }
                            })
                        }

                    }

                })
                .catch(e => {
                    console.log("失败")
                    fetch.hideLoadingText();
                    wx.showToast({
                        title: '支付失败',
                        icon: 'none',
                        duration: 2000,
                        mask: true,
                        'success': function() {

                        }
                    })

                })
        } else if (this.data.rechargeAmount == 100 || this.data.rechargeAmount > 100 && this.data.isShowMaiXiang) {

            this.maiXiangRecharge();
        }
    },
    maiXiangRecharge: function() {
        var _self = this;
        wx.showLoading({
            title: '加载中',
            mask: true
        })
        wx.request({
            url: getApp().globalData.baseUrl + '/applet/order/createReBalanceVoucherOrder.do',
            data: {
                rechargeAmount: _self.data.rechargeAmount,
                shopCode: getApp().globalData.common.shopCode,
            },
            header: getApp().globalData.header,
            success: function(res) {
                wx.hideLoading();
                console.log("****")
                console.log(res)
                if (res.data.code != 200) {
                    wx.showToast({
                        title: '支付失败',
                        icon: 'none',
                        duration: 2000,
                        mask: true,
                        'success': function() {}
                    })

                } else {
                    if (res.data.obj.type == '0') {
                        wx.requestPayment({
                            'timeStamp': res.data.obj.timeStamp,
                            'nonceStr': res.data.obj.nonceStr,
                            'package': res.data.obj.prepay_id,
                            'signType': res.data.obj.signType,
                            'paySign': res.data.obj.paySign,
                            'success': function(response) {
                                wx.showToast({
                                    title: '充值成功,预计10分钟内到账',
                                    icon: 'none',
                                    duration: 2000,
                                    mask: true,
                                    'success': function() {
                                        setTimeout(() => {
                                            wx.switchTab({
                                                url: '../mine/mine'
                                            })
                                        }, 2000)
                                    }
                                })

                            },
                            'fail': function(res) {
                                console.log(res)
                                wx.showToast({
                                    title: '取消支付',
                                    icon: 'none',
                                    duration: 1500
                                })
                            }
                        })

                    } else if (res.data.obj.type == '1') {
                        console.log('微信代扣')
                        //type=1:微信代扣
                        if (res.data.obj.status == 1) {
                            wx.showToast({
                                title: '充值成功,预计10分钟内到账',
                                icon: 'none',
                                duration: 2000,
                                mask: true,
                                'success': function() {
                                    setTimeout(() => {
                                        wx.switchTab({
                                            url: '../mine/mine'
                                        })
                                    }, 2000)
                                }
                            })
                        } else if (res.data.obj.status == 4) {
                            wx.showToast({
                                title: '支付中...请稍后查看余额明细',
                                icon: 'none',
                                duration: 2000,
                                mask: true,
                                'success': function() {
                                    setTimeout(() => {
                                        wx.switchTab({
                                            url: '../mine/mine'
                                        })
                                    }, 2000)
                                }
                            })
                        } else {
                            wx.showToast({
                                title: '支付失败',
                                icon: 'none',
                                duration: 2000,
                                mask: true,
                                'success': function() {

                                }
                            })
                        }


                    }
                }

            }
        })
    },
    hideView: function() {

        this.setData({
            showFloat: false,
            showInfo: false
        })
    },
    showInfoAction: function() {
        this.setData({
            showFloat: true,
            showInfo: true
        })
    },
    makeSureRecharge: function() {

        this.recharge();
    }
})